package com.example.controller;


import com.example.entity.Ignin;
import com.example.entity.Legalperson;
import com.example.entity.note.HttpUtils;
import com.example.service.IIgninService;
import com.example.service.ILegalpersonService;
import com.example.service.ILegalpersonService;
import com.example.util.Code;
import com.example.util.Result;
import net.sf.jsqlparser.statement.create.table.Index;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2023-11-03
 */
@RestController
@RequestMapping("/s/ignin")
@CrossOrigin
public class IgninController {
    @Autowired
    private IIgninService iIgninService;
    @Autowired
    private OccController controller;
    @Autowired
    private ILegalpersonService iLegalpersonService;
    //短信验证码
    @PostMapping("/a")
    public void noteYzm(String phone) {
        String host = "https://gyytz.market.alicloudapi.com";
        String path = "/sms/smsSend";
        String method = "POST";
        String appcode = "2d208b78fca34922a11d3dec3fee4756";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "**code**:12345,**minute**:5");

//smsSignId（短信前缀）和templateId（短信模板），可登录国阳云控制台自助申请。参考文档：http://help.guoyangyun.com/Problem/Qm.html

        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从\r\n\t    \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t    \t* 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //注册
    @PostMapping("/addphone")
    public Result addPhone(@RequestBody Ignin ignin){
        //判断手机号格式是否正确
        String phoneNumber=ignin.getPhone();
        String regex = "^1[3456789]\\d{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phoneNumber);
        //插入数据
        if (matcher.matches()){
            int i = iIgninService.addPhone(ignin);
//          noteYzm(ignin.getPhone());
            return new Result(Code.SAVE_OK,ignin.getId(),"注册成功");
        }else {
            return new Result(Code.SAVE_ERR,ignin.getId(),"注册失败");
        }
    }
    @PostMapping("/find")
    public Result findPhone(@RequestBody Ignin ignin){
        Ignin phone = iIgninService.findPhone(ignin);
        if(phone!=null){
            return new Result(Code.GET_OK,phone.getId(),"登录成功");
        }else {
            return new Result(Code.GET_ERR,"请注册");
        }
    }
    //逻辑删除
    @GetMapping("/delete/{id}/{del}")
    public Result deleteById(@PathVariable Integer id,@PathVariable Integer del) {
        int i = iIgninService.deleteById(id,del);
        if (i>0){
            return new Result(Code.DELETE_OK,"已暂停合作");
        }else {
            return new Result(Code.DELETE_ERR,"暂停合作失败");
        }
    }
    @PostMapping("/ac/{zid}")
    public Result a(@RequestParam MultipartFile file, @PathVariable Integer  zid) throws IOException {
        Result result = controller.uploadImg(file);
        Legalperson legalperson = new Legalperson();
        legalperson.setZid(zid);
        legalperson.setZmz((String)result.getData());
        int i = iLegalpersonService.addzIdCard(legalperson);
        return new Result(Code.GET_OK,result.getData(),"成功");
    }
    @PostMapping("/ac1/{zid}")
    public Result a1(@RequestParam MultipartFile file, @PathVariable Integer zid) throws IOException {
        Result result = controller.uploadImg(file);
        Legalperson legalperson = new Legalperson();
        legalperson.setZid(zid);
        legalperson.setFmz((String)result.getData());
        int i = iLegalpersonService.addfIdCard(legalperson);
        return new Result(Code.GET_OK,result.getData(),"成功");
    }
    @GetMapping("/finddeleted/{id}")
    public int findDeleted(@PathVariable Integer id){
       return iIgninService.findDeleted(id);
    }
}
